package com.my_musc_service.controller.api;

import com.my_musc_service.common.R;
import com.my_musc_service.entity.Song;
import com.my_musc_service.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Auther: yuansong
 * @Date: 2021/6/15 21:35
 * @Description:
 */
@RestController
@RequestMapping(value = "upload")
public class UploadController {

    @Autowired
    private UploadService uploadService;


    @PostMapping()
    public R upload(@RequestParam("songFile") MultipartFile songFile,
                    @RequestParam(value = "lyricFile", required = false) MultipartFile lyricFile,
                    @RequestParam(value = "coverFile", required = false) MultipartFile coverFile,
                    @RequestParam(value = "name", required = true) String name,
                    @RequestParam(value = "singer", required = true) String singer,
                    @RequestParam(value = "description", required = false) String description, HttpServletRequest request) {

        Song song = new Song();
        song.setName(name);
        song.setSinger(singer);
        song.setDescript(description);
        String songFileURL = uploadService.upload(songFile, lyricFile,coverFile, song, request);
        return R.ok().data("songFileURL", songFileURL);

    }

    @PostMapping("singer")
    public R uploadSinger(
            @RequestParam(value = "singer", required = true) String singer,
            @RequestParam(value = "singerImg", required = true) MultipartFile singerImg
    ) {

        String singerImgURL = uploadService.uploadSingerImg(singer, singerImg);
        return R.ok().data("singerImgURL", singerImgURL);

    }

    @PostMapping("headerImg")
    public R upload(
            @RequestParam(value = "userId", required = true) Integer userId,
            @RequestParam(value = "headerImg", required = true) MultipartFile headerImg
    ) {

        String headerImgURL = uploadService.uploadHeaderImg(userId, headerImg);
        return R.ok().data("headerImgUrl", headerImgURL);

    }
}
